function Plane(image, shadowImage, x, y, bullets, missiles) {
  this.image = image;
  this.shadowImage = shadowImage;
  this.x = x;
  this.y = y;
  this.speed = 0;
  this.angle = 0;
  this.speedChange = false;

  //this.trail = [{x: this.x, y: this.y}];
  //this.trailTimer = 0;
  this.trail1 = new Trail();
  this.trail2 = new Trail();

  this.bullets = bullets;
  this.missiles = missiles;

  this.minSpeed = 500;
  this.normalSpeed = 1000;
  this.maxSpeed = 1500;
}

Plane.prototype.changeSpeed = function(amount) {
  this.speedChange = true;
  this.speed += amount;
  if (this.speed > this.maxSpeed) this.speed = this.maxSpeed;
  else if (this.speed < this.minSpeed) this.speed = this.minSpeed;
}

Plane.prototype.changePitch = function(amount) {
  this.angle += amount;
  this.angle = this.angle % (2 * Math.PI);
}

Plane.prototype.update = function(dt) {
  if (!this.speedChange) {
    if (this.speed < this.normalSpeed) {
      this.speed += 200 * dt;
      if (this.speed > this.normalSpeed) this.speed = this.normalSpeed;
    } else if (this.speed > this.normalSpeed) {
      this.speed -= 200 * dt;
      if (this.speed < this.normalSpeed) this.speed = this.normalSpeed;
    }
  } else {
    this.speedChange = false;
  }

  this.x += this.speed * Math.cos(this.angle) * dt;
  this.y += this.speed * Math.sin(this.angle) * dt;

  var x = this.x - (this.image.width/2 - 10) * Math.cos(this.angle - 0.6);
  var y = this.y - (this.image.width/2 - 10) * Math.sin(this.angle - 0.6);        
  this.trail1.update(x, y, dt);
  this.trail2.update(this.x, this.y, dt);

  /*this.trailTimer += dt;
  if (this.trailTimer > 0.05) {
    this.trailTimer = 0;
    var x = this.x - (this.image.width/2 + 20) * Math.cos(this.angle);
    var y = this.y - (this.image.width/2 + 20) * Math.sin(this.angle);    
    this.trail.push({x: x, y: y});
    if (this.trail.length > 20) {
      this.trail.shift();
    }
  }*/
}

Plane.prototype.render = function(context) {
  if (this.y + context.canvas.height/2 > 0) {
    var radius = 50 + this.y * 0.15;
    if (radius > 50) radius = 50;
    if (radius > 0) {
      context.save();
      context.translate(this.x, 0);
      context.scale(1, 0.25);
      context.beginPath();
      context.arc(0, 0, radius, 0, 2 * Math.PI, false);
      /*var gradient = context.createRadialGradient(
        0, 0, radius * 0.25, 0, 0, radius);
      gradient.addColorStop(0, 'rgba(0, 0, 0, 0.4)');
      gradient.addColorStop(1, 'rgba(0, 0, 0, 0.0)');*/
      context.fillStyle = 'rgba(0, 0, 0, 0.4)';
      context.fill();
      context.restore();
    }
  }

  this.trail2.render(context);

  context.save();
  context.translate(this.x, this.y);
  context.rotate(this.angle);

  context.drawImage(this.image, -this.image.width/2, -this.image.height/2);

  var scaleX = this.speed / this.normalSpeed;
  scaleX *= scaleX;
  var scaleY = (scaleX > 1.0) ? 1.0 : scaleX;
  //context.fillStyle = 'rgba(255, 128, 128, 0.4)';
  // create radial gradient
  var gradient = context.createRadialGradient(-10, 0, 0, -10, 0, 60);
  gradient.addColorStop(0, 'rgba(255, 255, 50, 1.0)');
  gradient.addColorStop(0.1, 'rgba(255, 255, 50, 0.8)');  
  gradient.addColorStop(0.2, 'rgba(255, 50, 50, 1.0)');    
  gradient.addColorStop(1, 'rgba(255, 50, 50, 0.0)');
  context.fillStyle = gradient;

  context.beginPath();
  context.translate(-this.image.width/2, 0);
  context.scale(scaleX + Math.random() * 0.2, scaleY + Math.random() * 0.1);
  context.translate(5, 0);
  context.moveTo(-10, 10);
  context.quadraticCurveTo(-60, 0, -10, -10);
  context.quadraticCurveTo(0, 0, -10, 10);  
  context.fill();
  context.restore();

  this.trail1.render(context);
  /*context.lineCap = 'round';
  context.beginPath();
  var x = this.x - (this.image.width/2 + 20) * Math.cos(this.angle);
  var y = this.y - (this.image.width/2 + 20) * Math.sin(this.angle);
  context.moveTo(x, y);  
  for (var i = this.trail.length - 1; i >= 0; i--) {
    var alpha = 0.2; //i / this.trail.length * 1.0;
    context.strokeStyle = 'rgba(255, 255, 255, ' + alpha + ')';    
    context.lineWidth = i * this.speed / 600;
    context.lineTo(this.trail[i].x, this.trail[i].y);
    context.stroke();
    //context.beginPath();    
    //context.moveTo(this.trail[i].x, this.trail[i].y);  
  }*/
}

Plane.prototype.fireBullet = function() {
  var bullet_image = new Image();
  bullet_image.src = 'bullet.png';
  var bullet_speed = this.speed + 700;// + 200 * (2 * Math.random() - 1);
  var bullet_angle = this.angle + (2 * Math.random() - 1) * 0.5 * Math.PI/180;
  this.bullets.push(new Bullet(bullet_image, this.x, this.y, bullet_speed, bullet_angle));
}

Plane.prototype.fireMissile = function() { 
  var missile_image = new Image();
  var missile_x = this.x - 20 * Math.sin(this.angle);
  var missile_y = this.y + 20 * Math.cos(this.angle);
  this.missiles.push(new Missile(missile_image, missile_x, missile_y, this.speed, 
    this.angle + Math.random() * 0.01 - 0.005, this));
}
